#!/bin/bash

binDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${binDir} && cd ../
curDir=`pwd`

if [ ! -n "$PRO_PATH" ]; then
    export PRO_PATH="${curDir}"
fi

profiles=$2
rootDir=${curDir}
libPath=${rootDir}/lib
sourcePath=${rootDir}/conf
logPath=${rootDir}/logs

export PRO_PATH="${curDir}"
export ModuleName="crm-api"

if [ ! -d ${logPath} ]; then
    mkdir -p ${logPath}
fi
packageName=${curDir##*/}


start() {

 echo "start ${ModuleName}..."
    nohup java -Dloader.path=${libPath},${sourcePath} \
    -Dcom.sun.management.jmxremote.port=8889 \
    -Dcom.sun.management.jmxremote.authenticate=false \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Djava.rmi.server.hostname=192.168.36.128 \
    -Dcom.sun.management.jmxremote \
    -XX:+UseConcMarkSweepGC -Xmx2048m -Xms1024m -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:${logPath}/java_gc.log \
    -XX:-HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${logPath} -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8090 ${libPath}/${packageName}.jar \
    --spring.profiles.active=${profiles} --isJar=true &> ${logPath}/${ModuleName}.log &
}


stop() {
    pid=`ps -ef | grep ${libPath}/${packageName}.jar | grep -v grep | awk '{print $2}'`
    echo "stop ${ModuleName}..."
    if [ -n "${pid}" ]
    then
        kill -9 $pid
    fi

}

restart() {
    stop
    start
}

case "$1" in
	start|stop|restart)
  		case "$2" in
  		    haizhi|test|uat)
  		        $1
  		        ;;
  		    *)
  		        echo $"Usage: $0 {start|stop|restart} {dev|test|prod}"
  		        exit 2
  		esac
		;;
	*)
		echo $"Usage: $0 {start|stop|restart}"
		exit 1
esac